package com.wkk.schedul;

import java.util.Date;

import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * spring自带的类似轻量级quartz的定时调度
 * 
 * @author wkk
 *
 */
@RestController
@EnableScheduling	
public class Schedule implements SchedulingConfigurer {
	private String expression = "0/3 * * * * *";// 定时设定

	/**
	 * 对外提供 改变定时调用频率接口
	 * 
	 * @return
	 */
	@RequestMapping("/chnageExpression")
	public String changeExpression() {
		expression = "0/1 * * * * *";
		return expression;

	}

	@Override
	public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
		Runnable task = new Runnable() {
			@Override
			public void run() {
				// 加入要调用的方法

			}
		};
		Trigger trigger = new Trigger() {
			@Override
			public Date nextExecutionTime(TriggerContext triggerContext) {
				CronTrigger cronTrigger = new CronTrigger(expression);
				return cronTrigger.nextExecutionTime(triggerContext);
			}
		};
		scheduledTaskRegistrar.addTriggerTask(task, trigger);

	}

}
